<?php
/**
 * 功能描述：	系统权限管理
 * @datetime	2022-07-12
 * @version   	v2.3
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_PowerController extends Controller {

    // 是否登录
    function __construct($controller, $action) {
        parent::__construct($controller, $action);
        $loginSer = new Admin_LoginServe();
        $loginSer->judgeSessionLogin($controller, $action);
    }

    /**
     * Notes:角色列表
     */
    function index(){
        $mouldCode = 'sysrole';
        // 接收参数
        $pageNo = Fun::input('p', 1, 'd');
        $search = isset($_POST['search']) ? json_decode($_POST['search'], true) : array();
        // 列表
        $powerSer = new Admin_PowerServe();
        $result = $powerSer->getRoleListsPage($mouldCode, $pageNo, $search);
        // 用户权限信息
        $powerInfo = $powerSer->getUserPowerInfo();
        // 映射
        $this->set('powerInfo', $powerInfo);
        $this->set('statusList', BaseData::$statusArr2);
        $this->set('userLevel', getUserLevel());
        $this->set('searchInfo', $result['where']);
        $this->set('list', $result['list']);
        $this->set('pages', $result['pages']);
        $this->set('title', '角色列表');
        $this->set('mouldCode', $mouldCode);
        $this->display('role');
    }

    /**
     * Notes:权限功能列表
     */
    public function funs(){
        $mouldCode = 'syspower';
        // 接收参数
        $pageNo = Fun::input('p', 1, 'd');
        $search = isset($_POST['search']) ? json_decode($_POST['search'], true) : array();
        // 列表
        $powerSer = new Admin_PowerServe();
        $result = $powerSer->getFunsListsPage($mouldCode, $pageNo, $search);
        if(!is_array($result)){
            htmlAlert($result);
        }
        $classSer = new Admin_MenuServe();
        $classlist = $classSer->getClassList(0);
        // 映射
        $this->set('classlist', $classlist);
        $this->set('classlistFuns', BaseData::$powerClassArr);
        $this->set('statusList', BaseData::$statusArr2);
        $this->set('searchInfo', $result['where']);
        $this->set('list', $result['list']);
        $this->set('pages', $result['pages']);
        $this->set('title', '权限功能列表');
        $this->set('mouldCode', $mouldCode);
        $this->display('funs');
    }

    /**
     * Notes:导出Execl表格
     * User: ZhuHaili
     */
    function execl(){
        $pageNo = Fun::input('p', 0, 'd');
        $idStr = Fun::input('i');
        $search = Fun::input('search');
        $orderby = Fun::input('o'); //排序例如：a.id_desc,a.viewseq_desc
        $mouldCode = Fun::input('m');
        if($orderby){$orderby = strtr($orderby, '_', " ");} // 替换字符串
        // 获取数据
        $powerSer = new Admin_PowerServe();
        if(!$mouldCode){
            htmlAlert('请指定要导出的数据类型');
        }
        $fileTitle = '权限';
        if($mouldCode == 'sysrole'){
            $fileTitle = '权限角色';
            $thList['title'] = '角色';
            $thList['statustxt'] = '状态';
        }else if($mouldCode == 'syspower'){
            $fileTitle = '权限功能';
            $thList['title'] = '功能名称';
            $thList['funcode'] = '功能编码';
            $thList['menu_id'] = '所属菜单ID(0为全部,字母为类)';
            $thList['types'] = '类型(1菜单2文章3其他)';
            $thList['flag'] = '展示类型(1开发者2管理员)';
            $thList['statustxt'] = '状态';
        }
        $result = $powerSer->getListExecl($mouldCode, $pageNo,$idStr, $search, $thList, $orderby);
        // 导出execl
        $fileTitle = $fileTitle.'_'.date('Ymd-His'); // execl文件名
        $e = array('id'); // 屏蔽科学计数法字段
        $csv = new ManageCSV();
        $csv->export($result['trList'], $result['thList'], $fileTitle, $e);
    }

    /**
     * Notes:添加功能
     * User: ZhuHaili
     * Date: 2022/8/29
     */
    public function addfuns(){
        $subFlag = Fun::input('subFlag');
        // 提交数据
        if($subFlag == 'Y'){
            $param = Fun::input();
            $powerSer = new Admin_PowerServe();
            $result = $powerSer->addData('syspower', $param);
            Fun::json($result, true);
        }
        // 初始进入时映射页面
        $classSer = new Admin_MenuServe();
        $classlist = $classSer->getClassList(0);
        $this->set('classlist', $classlist);
        $this->set('classlistFuns', BaseData::$powerClassArr);
        $this->set('userLevel', getUserLevel());
        $this->set('powerTypeFlags', BaseData::$powerTypeFlags);
        $this->set('powerTypeArr', BaseData::$powerTypeArr);
        $this->set('statusArr', BaseData::$statusArr);
        $this->set('title', '添加功能权限');
        $this->display();
    }

    /**
     * Notes:添加角色
     * User: ZhuHaili
     * Date: 2022/8/29
     */
    public function addrole(){
        $subFlag = Fun::input('subFlag');
        // 提交数据
        if($subFlag == 'Y'){
            $param = Fun::input();
            $powerSer = new Admin_PowerServe();
            $result = $powerSer->addData('sysrole', $param);
            Fun::json($result, true);
        }
        // 初始进入时映射页面
        $classSer = new Admin_MenuServe();
        $classlist = $classSer->getClassList(0);
        $this->set('classlist', $classlist);
        $this->set('statusArr', BaseData::$statusArr);
        $this->set('title', '添加角色');
        $this->display();
    }

    /**
     * Notes:修改权限功能
     * User: ZhuHaili
     */
    public function updatefuns(){
        $powerSer = new Admin_PowerServe();
        $subFlag = Fun::input('subFlag');
        // 提交数据
        if($subFlag == 'Y'){
            $param = Fun::input();
            $result = $powerSer->updateData('syspower', $param);
            Fun::json($result, true);
        }
        // 初始进入时映射页面
        $id = Fun::input('i',0,'d');
        $info = $powerSer->getEssayInfo('syspower', array('id'=>$id));
        $classSer = new Admin_MenuServe();
        $classlist = $classSer->getClassList(0);
        $this->set('info', $info);
        $this->set('classlist', $classlist);
        $this->set('classlistFuns', BaseData::$powerClassArr);
        $this->set('powerTypeFlags', BaseData::$powerTypeFlags);
        $this->set('powerTypeArr', BaseData::$powerTypeArr);
        $this->set('statusArr', BaseData::$statusArr);
        $this->set('title', '修改功能权限');
        $this->display();
    }

    /**
     * Notes:修改角色
     * User: ZhuHaili
     */
    public function updaterole(){
        $powerSer = new Admin_PowerServe();
        $subFlag = Fun::input('subFlag');
        $powerClassId = Fun::input('c');
        // 提交数据
        if($subFlag == 'Y'){
            $param = Fun::input();
            $result = $powerSer->updateRoleData('sysrole', $param);
            Fun::json($result, true);
        }
        // 初始进入时映射页面
        $id = Fun::input('i',0,'d');
        $info = $powerSer->getEssayInfo('sysrole', array('id'=>$id));
        $classSer = new Admin_MenuServe();
        $classlist = $classSer->getClassList(0);
        $this->set('info', $info);
        $this->set('classlist', $classlist);
        $this->set('powerClassId', $powerClassId);
        $this->set('statusArr', BaseData::$statusArr);
        $this->set('title', '修改角色');
        $this->display();
    }

    /**
     * Notes:删除角色
     * User: ZHL
     * Date: 2022/7/18
     */
    public function delrole(){
        $commC = new Admin_CommonController();
        $commC->del();
    }

    /**
     * Notes:删除功能
     * User: ZHL
     * Date: 2022/7/18
     */
    public function delfuns(){
        $commC = new Admin_CommonController();
        $commC->del();
    }

    /**
     * Notes:批量排序
     * User: ZHL
     */
    public function seq(){
        $commC = new Admin_CommonController();
        $commC->seq();
    }

    /**
     * Notes:分配权限
     * User: ZhuHaili
     */
    public function giverule(){
        $powerSer = new Admin_PowerServe();
        $subFlag = Fun::input('subFlag');
        // 提交数据
        if($subFlag == 'Y'){
            $param = Fun::input();
            $result = $powerSer->roleGiveRule($param);
            Fun::json($result, true);
        }
        // 初始进入时映射页面
        $id = Fun::input('i',0,'d');
        $info = $powerSer->getEssayInfo('sysrole', array('id'=>$id));
        $cSer = new Admin_CommonModel();
        $rule = $cSer->getEssayList('syspower', array('status'=>1));
        $classSer = new Admin_MenuServe();
        $classlist = $classSer->getClassList(-1);
        $this->set('info', $info);
        $this->set('rule', $rule);
        $this->set('classlist', $classlist);
        $this->set('classlistFuns', BaseData::$powerClassArr);
        $this->set('title', '角色分配权限');
        $this->display();
    }

}